<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="请假类型" prop="leave_type">
            <el-select
              v-model="queryParams.leave_type"
              placeholder="请选择"
              clearable
              style="width: 215px"
            >
              <el-option
                v-for="dict in dict.type.bd_leave_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请时间" prop="dateRangeTime">
            <el-date-picker
              v-model="dateRangeTime"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
       <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bd:partner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bd:partner:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bd:partner:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LeaveApplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="请假人" align="center" prop="user_id" >
      </el-table-column>
      <el-table-column label="请假类型" align="center" prop="leave_type" >
<!--        <template slot-scope="scope">
          <dict-tag :options="dict.type.bd_leave_type" :value="scope.row.leave_type"/>
        </template> -->
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="apply_time" />
      <el-table-column label="起始时间" align="center" prop="start_time" />
      <el-table-column label="结束时间" align="center" prop="end_time" />
<!--      <el-table-column label="实际起始时间" align="center" prop="reality_start_timee" />
      <el-table-column label="实际结束时间" align="center" prop="reality_end_time" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['bd:partner:edit']">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['bd:partner:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="660px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="请假人" prop="user_id">
              <el-input v-model="form.user_id" placeholder="请输入请假人姓名" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假类型" prop="leave_type">
              <el-select
                v-model="form.leave_type"
                placeholder="请选择请假类型"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.bd_leave_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门领导" prop="dept_leader">
              <el-select
                v-model="form.dept_leader"
                placeholder="请选择部门领导"
                clearable
              >
                <el-option
                  v-for="(item,index) in deptLeaderList"
                  :key="index"
                  :label="item.user_name"
                  :value="item.user_name">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起始时间" prop="start_time">
              <el-date-picker
                v-model="form.start_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="请选择起始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="end_time">
              <el-date-picker
                v-model="form.end_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="请选择结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="原因" prop="reason">
              <el-input v-model="form.reason" type="textarea" placeholder="请输入请假原因" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">返回</el-button>
        <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getLoginer,
    listLeaveApply,
    listDeptLeader,
    addLeaveApply,
    updateLeaveApply,
    delLeaveApply,
    getLeaveApply,
  } from "../api/leave";
  export default {
    name: "LeaveApply",
    dicts: ['dept_leader','bd_leave_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //查询申请时间范围
        dateRangeTime: [],
        //部门领导列表
        deptLeaderList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          user_id: null,
          start_time: null,
          end_time: null,
          leave_type: null,
          reason: null,
          apply_time: null,
          reality_start_time: null,
          reality_end_time: null
        },
        user_ids: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {

        }
      };
    },
    created() {
      this.queryParams= this.$route.query;//传递路由上的参数进行分类查询
      this.getList();
      this.getDeptLeaderList();
    },
    methods: {
      /** 查询请假列表 */
      getList() {
      this.loading = true;
      if (null != this.dateRangeTime && '' != this.dateRangeTime) {
        this.queryParams["beginApplyTime"] = this.dateRangeTime[0];
        this.queryParams["endApplyTime"] = this.dateRangeTime[1];
        }
        listLeaveApply(this.queryParams).then(response => {
          this.LeaveApplyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
/*        listDeptLeader().then(response => {
          this.deptLeaderList = response.rows;
          this.total = response.total;
          this.loading = false;
        }); */
      },
     /* 查询部门领导列表*/
      getDeptLeaderList(){
        this.loading = true;
        listDeptLeader().then(response => {
          this.deptLeaderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.openPrivilege = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          user_id: null,
          start_time: null,
          end_time: null,
          leave_type: null,
          reason: null,
          apply_time: null,
          reality_start_time: null,
          reality_end_time: null,
          // date: [start_time, end_time]
        };
        this.autoGenFlag = false;
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.leave_type=null;
        this.dateRangeTime = [];
        this.queryParams["beginApplyTime"]=this.dateRangeTime[0];
        this.queryParams["endApplyTime"]=this.dateRangeTime[1];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增请假";
        this.optType = "add";
        //获取当前登录用户信息
        getLoginer().then(response => {
          this.form.user_id = response;
        })
      },
      /** 修改按钮操作 */
     handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getLeaveApply(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改请假";
          this.optType = "edit";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateLeaveApply(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addLeaveApply(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 查看详情按钮操作 */
      handleUpdate(row) {
        //console.log("8888888");
        this.reset();
        const id = row.id || this.ids
        console.log(id);
        getLeaveApply(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "请假详情";
          this.optType = "edit";
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除请假ids为"' + ids + '"的数据项？').then(function() {
          return delLeaveApply(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        alert("该部分功能尚未开发");
/*        this.download('md/client/export', {
          ...this.queryParams
        }, `client_${new Date().getTime()}.xlsx`) */
      },
    }
  };
</script>
